package com.xiaochen.sjzremind.utils;

import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.HashMap;
import java.util.Map;

/**
 * @author xc
 * @date 2025/10/09/14:57
 * @desc dom4j工具类
 */
@Slf4j
public class JsoupUtil {

    public static Map<String, Integer> getAllInfo(String url, String selector) {
        Map<String, Integer> map = new HashMap<>();
        try {
            // 使用jsoup解析HTML
            Document document = Jsoup.connect(url).get();
            // jsoup使用CSS选择器而非XPath
            Elements elements = document.select(selector);
            for (Element element : elements) {
                //log.info(element.toString());
                String id = element.attr("href").replaceAll(".*/(\\d+)$", "$1");
                String title = element.attr("title").replaceAll(".*【(.*)】.*", "$1");
                map.put(title, Integer.valueOf(id));

            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return map;
    }
}
